<script lang="ts" setup>
import type { BaseEmployeeModel } from '#/api';

import { reactive } from 'vue';

import { useVbenModal } from '@vben/common-ui';

import { useFs } from '@fast-crud/fast-crud';

import { BaseEmployeeApi } from '#/api';
import { useMessage } from '#/hooks/web/useMessage';

import { createCrudOptions } from '../data/invitation-crud';

defineOptions({
  name: '邀请用户',
  inheritAttrs: false,
});
const emit = defineEmits<Emits>();
interface Emits {
  (e: 'success'): void;
}
const formData = reactive({
  selectedRowKeys: [],
});

const { createMessage } = useMessage();
const { crudRef, crudBinding } = useFs({
  createCrudOptions,
  context: formData,
});

const [Modal, modalApi] = useVbenModal({
  async onOpened() {},
  onConfirm,
});

async function onConfirm() {
  if (!formData.selectedRowKeys || formData.selectedRowKeys.length === 0) {
    createMessage.warning('请选择待邀请用户');
    return;
  }

  try {
    modalApi.setState({ loading: true, confirmLoading: true });
    const params: BaseEmployeeModel.DefTenantBindUserVO = {
      userIdList: formData.selectedRowKeys,
      isBind: true,
    };
    await BaseEmployeeApi.invitationUser(params);
    createMessage.success('邀请成功');
    modalApi.close();
    emit('success');
  } finally {
    modalApi.setState({ loading: false, confirmLoading: false });
  }
}

defineExpose(modalApi);
</script>

<template>
  <Modal title="邀请用户" class="h-full w-full">
    <FsCrud ref="crudRef" v-bind="crudBinding" />
  </Modal>
</template>
